package com.servlet;

import com.dao.MsgUserInfoDao;
import com.dao.impl.MsgUserInfoDaoImpl;
import com.entity.MsgUserInfo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

public class UserServlet extends HttpServlet {



    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String action = req.getParameter("action");
        MsgUserInfoDao msgUserInfoDao = new MsgUserInfoDaoImpl();
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        if("login".equals(action)){
            String name = req.getParameter("username");
            String pwd = req.getParameter("password");

            MsgUserInfo msgUserInfo = msgUserInfoDao.login(name, pwd);
            if(msgUserInfo !=null){
                //��¼�ɹ����ŵ�session����

                session.setAttribute("loginuser",msgUserInfo.getUsername());
                //��Ȼ�Ѿ��ŵ�session������ ����ת�����ض��������ν��
                resp.sendRedirect("MsgSerlvet?action=findMsg");
            }else{
                //��¼ʧ�� ������ʾ
                req.setAttribute("error","�û�����������������󣡣���");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }
        }else if("regist".equals(action)){
            //ע�����
            String name = req.getParameter("username");
            String pwd = req.getParameter("password");
            String email = req.getParameter("email");

            MsgUserInfo user = msgUserInfoDao.findByName(name);
            if(user != null){
                //���� ����ע��
                req.setAttribute("error","�û����Ѿ�ע�ᣡ������");
                req.getRequestDispatcher("register.jsp").forward(req,resp);
            }else{
                //�����ڣ�����ע�ᣬ  ����ע�᷽���Ϳ�����
                MsgUserInfo u = new MsgUserInfo();
                u.setUsername(name);
                u.setPassword(pwd);
                u.setEmail(email);
                int res = msgUserInfoDao.addUser(u);
                if(res > 0){
                    resp.sendRedirect("index.jsp");
                }else{
                    req.setAttribute("error","ע��ʧ�ܣ�������");
                    req.getRequestDispatcher("register.jsp").forward(req,resp);
                }
            }
        }else if("logout".equals(action)){
            //�˳���¼
            session.removeAttribute("loginuser");
            resp.sendRedirect("index.jsp");
        }else if("checkName".equals(action)){
        	String name = req.getParameter("name");
        	System.out.println(name);
        	MsgUserInfo user = msgUserInfoDao.findByName(name);
        	PrintWriter pw = resp.getWriter();
        	pw.print(user);
        	/*if(user != null){
        		pw.print(false);
        	}else{
        		pw.print(true);
        	}*/
        }




    }
}
